using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Commerce.Common;
using Commerce.Providers;
using System.Collections.Generic;
using GCheckout.Checkout;
using GCheckout.Util;


    public partial class CheckoutGoogle : System.Web.UI.Page {
        protected void Page_Load(object sender, EventArgs e) {


        }
        DeliveryOptionCollection GetShippingOptions(Order order) {
            //get the shipping choices
            PackageInfo package = new PackageInfo();
            package.FromZip = SiteConfig.ShipFromZip;
            package.FromCountryCode = SiteConfig.ShipFromCountryCode;

            //here as placeholders
            package.ToZip = "96714";
            package.ToCountryCode = "US";

            package.Weight = order.GetItemsWeight();
            package.Width = Convert.ToInt16(order.GetTotalWidth());
            package.Height = Convert.ToInt16(order.GetTotalHeight());
            package.Length = Convert.ToInt16(order.GetMaxLength());
            package.DimensionUnit = SiteConfig.DimensionUnit;
            package.PackagingBuffer = SiteConfig.ShipPackagingBuffer;
            //Create Dictionary args for future expansion options
            package.Args = new Dictionary<string, string>();

            Commerce.Providers.DeliveryOptionCollection options = Commerce.Providers.FulfillmentService.GetOptions(package);

            return options;
        }

        string GetStateAbbreviation(string zip) {
            string sOut = "";
            //get the state abbreviation based on the zip
            SubSonic.Query q = new SubSonic.Query("CSK_Util_zipCode").AddWhere("zipCode", zip);
            q.SelectList = "stateAbbreviation";
            object result = q.ExecuteScalar();

            if (result != null) {
                sOut = result.ToString();
            }
            return sOut;
        }

        protected void PostCartToGoogle(object sender, System.Web.UI.ImageClickEventArgs e) {
            CheckoutShoppingCartRequest Req = gc.CreateRequest();
            Order order = OrderController.GetCurrentOrder();
            order.TaxAmount = 0;
            //add the items for this order
            foreach (OrderItem item in order.Items) {
                Req.AddItem(item.ProductName, item.ProductDescription, item.PricePaid, item.Quantity);
            }

            //add in the shipping options. This uses flat-rate shipping'
            //adjust as needed
            DeliveryOptionCollection options = GetShippingOptions(order);
            foreach (DeliveryOption option in options) {
                Req.AddFlatRateShippingMethod(option.Service, option.Rate);
            }

            //add in the tax rules
            IDataReader rdr = new SubSonic.Query("CSK_Tax_Rate").ExecuteReader();
            while (rdr.Read()) {
                double dRate = 0;
                Double.TryParse(rdr["rate"].ToString(), out dRate);
                Req.AddStateTaxRule(rdr["state"].ToString(), dRate, false);
            }

            //add in the order ID
            Req.MerchantPrivateData = "<orderguid>"+order.OrderGUID.ToString()+"</orderguid>";
            //Req.AnalyticsData = order.OrderID.ToString();
            
            //setup URLs
            Req.ContinueShoppingUrl = Utility.GetSiteRoot();
            Req.EditCartUrl = Utility.GetSiteRoot() + Page.ResolveUrl("~/basket.aspx");

            GCheckoutResponse Resp = Req.Send();
            Response.Redirect(Resp.RedirectUrl, true);

        }
    }
